//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "MVTTraining\fn_registerTargets.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_target","_marker_name","_street_shape","_crossings_shapes","_trigger","_shape","_pos","_targets","_inc","_diff","_street_dir","_target_dir"];

if (count GVAR(TARGETS) > 0) then {
	_square_shape = ["MVT_SQUARE"] call tmf_fnc_toShape;
	_square_in_shape = ["MVT_SQUARE_IN"] call tmf_fnc_toShape;
	_square_out_shape = ["MVT_SQUARE_IN"] call tmf_fnc_toShape;
	
	// "MVT_Square_Exit_1
	// GVAR(SQUARE_EXITS)
	
	_street_shape = ["MVT_STREET"] call tmf_fnc_toShape;
	_street_dir = markerDir "MVT_STREET";
	_street_dir = (abs(_street_dir)) mod 360;
	_crossings_shapes = [];
	_inc = 0;
	TRACE_2("Street Shape",_street_shape,_street_dir);
	
	for [{_inc = 1},{_inc <= GVAR(CROSSINGS)},{_inc = _inc + 1}] do {
		_marker_name = format["MVT_crossing_%1",_inc];		
		_crossings_shapes set [_inc, ([_marker_name] call tmf_fnc_toShape)];
		
		_trigger = [_marker_name] call tmf_fnc_markerToTrigger;
		_trigger setTriggerActivation ["ANY", "PRESENT", true];
		GVAR(CROSSINGS_STATUS) set [_inc,false];
		_trigger setTriggerStatements ["({(isPlayer _x) && {count ([_x] unitsBelowHeight 1.5) > 0}} count thisList) > 0",format["%2 set [%1, true];",_inc,QGVAR(CROSSINGS_STATUS)],format["%2 set [%1, false];",_inc,QGVAR(CROSSINGS_STATUS)]];
		GVAR(CROSSINGS_TRIGGERS) set [_inc, _trigger];
		sleep 0.1;
	};
	TRACE_1("Crossing Shapes",_crossings_shapes);
	TRACE_1("Crossing Triggers",GVAR(CROSSINGS_TRIGGERS));
	
	for [{_inc = 1},{_inc <= GVAR(BUILDINGS)},{_inc = _inc + 1}] do {
		_marker_name = format["MVT_building_%1",_inc];		
		
		_trigger = [_marker_name] call tmf_fnc_markerToTrigger;
		_trigger setTriggerActivation ["ANY", "PRESENT", true];
		GVAR(BUILDINGS_STATUS) set [_inc,false];
		_trigger setTriggerStatements ["({(isPlayer _x) && {count ([_x] unitsBelowHeight 1.5) > 0}} count thisList) > 0",format["%2 set [%1, true];",_inc,QGVAR(BUILDINGS_STATUS)],format["%2 set [%1, false];",_inc,QGVAR(BUILDINGS_STATUS)]];
		GVAR(BUILDINGS_TRIGGERS) set [_inc, _trigger];
		sleep 0.1;
	};
	TRACE_1("Building Triggers",GVAR(BUILDINGS_TRIGGERS));

	// Initialize the global variables
	GVAR(CROSSINGS_TARGETS) = [];
	GVAR(CROSSINGS_STREET_TARGETS_FRONT) = [];
	GVAR(CROSSINGS_STREET_TARGETS_BACK) = [];
	for [{_inc = 1},{_inc <= GVAR(CROSSINGS)},{_inc = _inc + 1}] do {
		GVAR(CROSSINGS_TARGETS) set[_inc,[]];
		GVAR(CROSSINGS_STREET_TARGETS_FRONT) set [_inc,[]];
		GVAR(CROSSINGS_STREET_TARGETS_BACK) set [_inc,[]];
	};
	
	{
		_target = _x;
		_target setVariable["IsUp",false];
		_target setVariable["UpTime",0];
		_target setVariable["PopUp",-1];
		_target enableSimulation False;
		_target addEventHandler ["HandleDamage", FUNC(targetShot)];
		_target animate["terc", 1];
		sleep 0.1;
		_pos = position _target;
		_shape = [];
		TRACE_1("Register",ARR_2(_target,typeof _target));
		for [{_inc = 1},{_inc <= GVAR(CROSSINGS)},{_inc = _inc + 1}] do {
			_shape = _crossings_shapes select _inc;
			if ([_pos,_shape] call tmf_fnc_inShape) exitWith {
				if ([position _target, _street_shape] call tmf_fnc_inShape) then {
					_target_dir = (abs(getDir _target)) mod 360;
					_diff = abs(_street_dir - _target_dir);
					if (_diff < 90 || _diff > 270) then {
						_targets = GVAR(CROSSINGS_STREET_TARGETS_FRONT) select _inc;
						_targets set [count _targets, _target];
						GVAR(CROSSINGS_STREET_TARGETS_FRONT) set [_inc,_targets];
						TRACE_1("Street Target Front",ARR_3(_target,_inc,_target_dir));
					} else {
						_targets = GVAR(CROSSINGS_STREET_TARGETS_BACK) select _inc;
						_targets set [count _targets, _target];
						GVAR(CROSSINGS_STREET_TARGETS_BACK) set [_inc,_targets];
						TRACE_1("Street Target Back",ARR_3(_target,_inc,_target_dir));
					};
				} else {
					_targets = GVAR(CROSSINGS_TARGETS) select _inc;
					_targets set [count _targets, _target];
					GVAR(CROSSINGS_TARGETS) set[_inc,_targets];
					TRACE_1("Crossing Target",ARR_2(_target,_inc));
				};
			};
		};
	} foreach GVAR(TARGETS);
	TRACE_1("Registered Targets",count GVAR(TARGETS));
	#ifdef __FORCE_TRACE__
	for [{_inc = 1},{_inc <= GVAR(CROSSINGS)},{_inc = _inc + 1}] do {
		diag_log format["Crossing %1: %2",_inc, GVAR(CROSSINGS_TARGETS) select _inc];
		diag_log format["Street F %1: %2",_inc, GVAR(CROSSINGS_STREET_TARGETS_FRONT) select _inc];
		diag_log format["Street B %1: %2",_inc, GVAR(CROSSINGS_STREET_TARGETS_BACK) select _inc];
	};
	#endif
};

TRACE("End");
TMFPROFILERSTOP;
